﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyDuAn.BUS;
using QuanLyDuAn.DTO;
using System.Collections;
namespace QuanLyDuAn.GUI
{
    public partial class FrmIterviewResult : DevComponents.DotNetBar.Office2007RibbonForm
    {
        InterviewerController interviewer = new InterviewerController();
        ApplicantController applicant = new ApplicantController();
        InterviewResultController resultinterview = new InterviewResultController();
        DataSet dsInterview, dsApplicant, dsResult;
        ArrayList list;
        public FrmIterviewResult()
        {
            InitializeComponent();
        }
        public void LoadForm()
        {
            dsResult = resultinterview.LoadInterviewResult();
            dataGridViewX1.DataSource = dsResult.Tables[0];
        }
        public bool CheckValueValid()
        {
            bool check = false;
            String interviewer = (String)cmbInterview.Text;
            String applicant = (String)cmbApplicant.Text;
            list = resultinterview.LoadResult();
            foreach (CheckResult j in list)
            {
                String applicantName = j.applicantName;
                String interviewerName = j.interviewerName;
                if ((interviewer.CompareTo(interviewerName) == 0) && (applicant.CompareTo(applicantName) == 0))
                {
                    check = false;
                    MessageBox.Show("This interview is exits !");
                    break;
                }
                else
                {
                    check = true;
                }
            }
            return check;
        }
        private bool checkInvalid()
        {
            bool check = false;
            String note = txtNote.Text;
            String resu = cmbResult.Text;
            if (resu.CompareTo("") == 0)
            {
                check = false;
                MessageBox.Show("Please insert Result");
            }
            else if (note.CompareTo("") == 0)
            {
                check = false;
                MessageBox.Show("Please insert Note!!");
            }
            else
            {
                check = true;
            }
            return check;
        }
        public void ResetForm()
        {
            txtNote.Text = "";
            cmbResult.Text = "";
        }
        public void InsertInterviewResult()
        {
            String interviewer = (String)cmbInterview.SelectedValue;
            String applicant = (String)cmbApplicant.SelectedValue;
            String result = cmbResult.SelectedItem.ToString();
            String note = txtNote.Text;
            resultinterview.InsertInterviewResult(interviewer, applicant, result, note);
            MessageBox.Show("Insert successful !!!");
        }
        public void UpdateInterviewResult()
        {
            String interviewer = (String)cmbInterview.SelectedValue;
            String applicant = (String)cmbApplicant.SelectedValue;
            String result = cmbResult.SelectedItem.ToString();
            String note = txtNote.Text;
            resultinterview.UpdateInterviewResult(interviewer, applicant, result, note);
            MessageBox.Show("Update successful !!!");
        }
        public void DeleteInterviewerResult()
        {
            String applicant = (String)cmbApplicant.SelectedValue;
            if (MessageBox.Show("You really want to delete?", "Notice", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                try
                {
                    resultinterview.DeleteInterviewResult(applicant);
                    MessageBox.Show("Del successfull", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    LoadForm();
                    ResetForm();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Errors");
                }
            }
        }
        private void FrmIterviewResult_Load(object sender, EventArgs e)
        {
            this.dataGridViewX1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dsResult = resultinterview.LoadInterviewResult();
            dataGridViewX1.DataSource = dsResult.Tables[0];
            dsInterview = interviewer.LoadInterviewer();
            cmbInterview.DataSource = dsInterview.Tables[0];
            cmbInterview.DisplayMember = dsInterview.Tables[0].Columns[3].ToString();
            cmbInterview.ValueMember = dsInterview.Tables[0].Columns[0].ToString();
            dsApplicant = applicant.LoadApplicant();
            cmbApplicant.DataSource = dsApplicant.Tables[0];
            cmbApplicant.DisplayMember = dsApplicant.Tables[0].Columns[3].ToString();
            cmbApplicant.ValueMember = dsApplicant.Tables[0].Columns[0].ToString();
            //this.LoadForm();
            btnEdit.Enabled = false;
            btnDelete.Enabled = false;
            cmbResult.Items.Add("Reached");
            cmbResult.Items.Add("Not Reached");
        }


        private void btnAdd_Click(object sender, EventArgs e)
        {
            list = resultinterview.LoadResult();
            if (list.Count == 0 && this.checkInvalid() == true)
            {
                this.InsertInterviewResult();
                this.LoadForm();
            }
            else if (this.CheckValueValid() == true && this.checkInvalid() == true)
            {
                this.InsertInterviewResult();
                this.LoadForm();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            this.DeleteInterviewerResult();
            this.LoadForm();
        }

        private void dataGridViewX1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            btnDelete.Enabled = true;
            btnEdit.Enabled = true;
            DataGridViewRow row = new DataGridViewRow();
            row = dataGridViewX1.Rows[e.RowIndex];
            cmbInterview.Text = row.Cells[0].Value.ToString();
            cmbApplicant.Text = row.Cells[1].Value.ToString();
            cmbResult.SelectedItem = row.Cells[2].Value.ToString();
            txtNote.Text = row.Cells[3].Value.ToString();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            this.UpdateInterviewResult();
            this.LoadForm();
            this.ResetForm();
        }
    }
}
